package com.mf.morefun.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mf.morefun.MoreFunApplication;
import com.mf.morefun.R;
import com.mf.morefun.bean.DoHomeWorkListBean;
import com.mf.morefun.bean.HomeWorkBean;
import com.mf.morefun.listener.HomeWorkListListener;
import com.mf.morefun.listener.ReadDetailTypeListener;
import com.mf.morefun.util.IsLoginBean;
import com.zhcx.zhcxlibrary.utils.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeWorkAdapter extends BaseAdapter {
    private Context mContext;
    private List<DoHomeWorkListBean> mDoHomeWorkList;
    private HomeWorkListListener listener;
    private String userType;

    public HomeWorkListListener getListener() {
        return listener;
    }

    public void setListener(HomeWorkListListener listener) {
        this.listener = listener;
    }

    public HomeWorkAdapter(Context mContext, List<DoHomeWorkListBean> mDoHomeWorkList, String userType) {
        this.mContext = mContext;
        this.mDoHomeWorkList = mDoHomeWorkList;
        this.userType = userType;
    }

    @Override
    public int getCount() {
        return mDoHomeWorkList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDoHomeWorkList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_homeworkitem, null);
        } else {
            view = convertView;
        }
        if (null != mDoHomeWorkList && mDoHomeWorkList.size() > 0){
            ViewHolder holder = new ViewHolder(view);
            holder.tvName.setText(mDoHomeWorkList.get(position).getUserName());
            holder.tvHomework.setText("");
            if (null != mDoHomeWorkList.get(position).getMfTpLRR()){
                holder.tvHomework.setText(mDoHomeWorkList.get(position).getMfTpLRR().getName());
            }else {
                if (null != mDoHomeWorkList.get(position).getMfTpCreation()){
                    holder.tvHomework.setText(mDoHomeWorkList.get(position).getMfTpCreation().getName());
                }
            }
            if (null != mDoHomeWorkList.get(position).getMfTpRecord()){
                holder.tvTime.setText(mDoHomeWorkList.get(position).getMfTpRecord().getCreateDate());
            }else {
                holder.tvTime.setText("");
            }
            if (StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpComment())){
                holder.tvType.setText("无点评");
                holder.tvDo.setBackgroundResource(R.drawable.gray_shapes);
            }else {
                holder.tvType.setText("有点评");
                holder.tvDo.setBackgroundResource(R.drawable.login_bg);
            }
            holder.tvDo.setTag(position);
            holder.tvDo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != mDoHomeWorkList.get(position).getMfTpLRR()){
                        if (!StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpLRR().getType())){
                            if (mDoHomeWorkList.get(position).getMfTpLRR().getType().equals("3")){
                                //录音又分未完成和已完成
                                if (StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpRecordFlag())) {
                                    listener.callBack((int) v.getTag(), 3);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                } else {
                                    if (mDoHomeWorkList.get(position).getMfTpRecordFlag().equals("0")) {
                                        listener.callBack((int) v.getTag(), 3);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                    } else {
                                        listener.callBack((int) v.getTag(), 4);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                    }
                                }
                            }else if(mDoHomeWorkList.get(position).getMfTpLRR().getType().equals("4")){
                                listener.callBack((int) v.getTag(), 2);//直接去视频
                            }else {
                                if (StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpRecordFlag())) {
                                    listener.callBack((int) v.getTag(), 0);//创作 0  磨耳朵1  阅读2  录音3
                                } else {
                                    if (mDoHomeWorkList.get(position).getMfTpRecordFlag().equals("0")) {
                                        listener.callBack((int) v.getTag(), 0);//创作 0  磨耳朵1  阅读2  录音3
                                    } else {
                                        listener.callBack((int) v.getTag(), 1);//创作 0  磨耳朵1  阅读2  录音3
                                    }
                                }
                            }
                        }
                    }else {
                        if (null != mDoHomeWorkList.get(position).getMfVideo()){
                            listener.callBack((int) v.getTag(), 2);//直接去视频
                        }else {
                            if (null != mDoHomeWorkList.get(position).getMfTpCreation()) {
                                if (StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpRecordFlag())) {
                                    listener.callBack((int) v.getTag(), 0);//创作 0  磨耳朵1  阅读2  录音3
                                } else {
                                    if (mDoHomeWorkList.get(position).getMfTpRecordFlag().equals("0")) {
                                        listener.callBack((int) v.getTag(), 0);//创作 0  磨耳朵1  阅读2  录音3
                                    } else {
                                        listener.callBack((int) v.getTag(), 1);//创作 0  磨耳朵1  阅读2  录音3
                                    }
                                }
                            } else {
                                //录音又分未完成和已完成
                                if (StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpRecordFlag())) {
                                    listener.callBack((int) v.getTag(), 3);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                } else {
                                    if (mDoHomeWorkList.get(position).getMfTpRecordFlag().equals("0")) {
                                        listener.callBack((int) v.getTag(), 3);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                    } else {
                                        listener.callBack((int) v.getTag(), 4);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                    }
                                }
                            }
                        }
                    }
                }
            });
                if (StringUtils.isEmpty(userType)){
                    if (userType.equals("1")){
                        holder.tvDo.setText("查看");
                    }else {
                        holder.tvDo.setText("点评");
                    }
                }else{
                    holder.tvDo.setText("查看");
                }
            if (position == 0){
                holder.llBg.setBackgroundResource(R.color.nomalbg_color);
            }else if(position == (mDoHomeWorkList.size() - 1)){
                if ((position % 2) == 0){
                    holder.llBg.setBackgroundResource(R.color.nomalbg_color);
                }else {
                    holder.llBg.setBackgroundResource(R.drawable.white_bottomshape);
                }
            }else {
                if ((position % 2) == 0){
                    holder.llBg.setBackgroundResource(R.color.nomalbg_color);
                }else {
                    holder.llBg.setBackgroundResource(R.color.white);
                }
            }
        }
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_homework)
        TextView tvHomework;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.tv_do)
        TextView tvDo;
        @BindView(R.id.ll_bg)
        LinearLayout llBg;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
